<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>ec: include/ec/loop.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">ec
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li><li class="navelem"><a class="el" href="dir_086fd96841d95816c032b124cf477f3d.html">ec</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">loop.h</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * loop.h</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> *  Created on: 2015年5月30日</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> *      Author: havesnag</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;</div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#ifndef INCLUDE_EC_LOOP_H_</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="preprocessor"></span><span class="preprocessor">#define INCLUDE_EC_LOOP_H_</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="preprocessor"></span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;</div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="preprocessor">#include &lt;map&gt;</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="preprocessor">#include &quot;ec/define.h&quot;</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;</div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="keyword">namespace </span>ec</div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;{</div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;</div>
<div class="line"><a name="l00023"></a><span class="lineno"><a class="line" href="classec_1_1Loop.html">   23</a></span>&#160;<span class="keyword">class </span><a class="code" href="classec_1_1Loop.html">Loop</a></div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;{</div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;    <a class="code" href="classec_1_1Loop.html">Loop</a>();</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;    <span class="keyword">virtual</span> ~<a class="code" href="classec_1_1Loop.html">Loop</a>();</div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;</div>
<div class="line"><a name="l00030"></a><span class="lineno"><a class="line" href="classec_1_1Loop.html#ab104699b3b40815f32e5272e85fb01d7">   30</a></span>&#160;    <span class="keyword">inline</span> <span class="keyword">operator</span> event_base * () <span class="keyword">const</span></div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;    {</div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;        <span class="keywordflow">return</span> _base;</div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;    };</div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;</div>
<div class="line"><a name="l00036"></a><span class="lineno"><a class="line" href="classec_1_1Loop.html#af62f9eae725eda092cc52168bdcb9b53">   36</a></span>&#160;    <span class="keyword">inline</span> event_base * <a class="code" href="classec_1_1Loop.html#af62f9eae725eda092cc52168bdcb9b53">ev</a>()<span class="keyword"> const</span></div>
<div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;<span class="keyword">    </span>{</div>
<div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;        <span class="keywordflow">return</span> _base;</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;    }</div>
<div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;</div>
<div class="line"><a name="l00042"></a><span class="lineno"><a class="line" href="classec_1_1Loop.html#ae60730f9a1e07de6039de8008293769a">   42</a></span>&#160;    <span class="keyword">inline</span> uint32_t <a class="code" href="classec_1_1Loop.html#ae60730f9a1e07de6039de8008293769a">id</a>()<span class="keyword"> const</span></div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;<span class="keyword">    </span>{</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;        <span class="keywordflow">return</span> _id;</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;    }</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;</div>
<div class="line"><a name="l00048"></a><span class="lineno"><a class="line" href="classec_1_1Loop.html#a134b0eb8eaa83270eb375df11160bea5">   48</a></span>&#160;    <span class="keyword">inline</span> <span class="keywordtype">bool</span> <a class="code" href="classec_1_1Loop.html#a134b0eb8eaa83270eb375df11160bea5">isThread</a>()<span class="keyword"> const</span></div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;<span class="keyword">    </span>{</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;        <span class="keywordflow">return</span> (NULL != _thread);</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;    }</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;    <span class="keywordtype">bool</span> <a class="code" href="classec_1_1Loop.html#a5e4e5650fbd4f1716429205c84ca51fd">start</a>(<span class="keywordtype">bool</span> newThread = <span class="keyword">true</span>);</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;    <span class="keywordtype">void</span> <a class="code" href="classec_1_1Loop.html#a476329b5b1b32a9d91957fdf820ed35b">wait</a>();</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;    <span class="keywordtype">void</span> <a class="code" href="classec_1_1Loop.html#aa96af9467ac7882e3cea7a4dcb51005a">stop</a>(<span class="keywordtype">bool</span> waiting = <span class="keyword">true</span>);</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;</div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;<span class="keyword">protected</span>:</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;    <span class="keyword">virtual</span> <span class="keywordtype">bool</span> <a class="code" href="classec_1_1Loop.html#ab09acf916e8d3ad94cc2ceb483b1cac6">onBeforeStart</a>();</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classec_1_1Loop.html#ab50d820e5c09ef53af2aa97a1912025a">onBeforeLoop</a>();</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classec_1_1Loop.html#a9d3fec1a0d05dfdb73ceee20b6fd55b0">onAfterLoop</a>();</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;</div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classec_1_1Loop.html#aa52a4ac0232b307a08e7857748ebdd3f">onAfterStop</a>();</div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;<span class="keyword">private</span>:</div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;    <span class="keywordtype">void</span> run();</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;<span class="keyword">private</span>:</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;    uint32_t _id;</div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;    event_base *_base;</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;    <span class="comment">//线程</span></div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;    std::thread *_thread;</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;    <span class="comment">//停止标志</span></div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;    std::atomic&lt;bool&gt; _isStopping;</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;    <span class="keyword">static</span> <a class="code" href="classec_1_1Loop.html">Loop</a> * <a class="code" href="classec_1_1Loop.html#abb797bdabdefdff5b6cc3f2b1b249747">curLoop</a>();</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;</div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;    <span class="keyword">static</span> <a class="code" href="classec_1_1Loop.html">Loop</a> * <span class="keyword">get</span>(uint32_t <a class="code" href="classec_1_1Loop.html#ae60730f9a1e07de6039de8008293769a">id</a>);</div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;<span class="keyword">private</span>:</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;    <span class="keyword">static</span> std::mutex _sMutex;</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;    <span class="keyword">static</span> std::map&lt;uint32_t, ec::Loop *&gt; _sLoops;</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;    <span class="keyword">static</span> uint32_t _sIdGenerater;</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;};</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;</div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;} <span class="comment">/* namespace ec */</span></div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;<span class="preprocessor">#endif </span><span class="comment">/* INCLUDE_EC_LOOP_H_ */</span><span class="preprocessor"></span></div>
<div class="ttc" id="classec_1_1Loop_html_ab09acf916e8d3ad94cc2ceb483b1cac6"><div class="ttname"><a href="classec_1_1Loop.html#ab09acf916e8d3ad94cc2ceb483b1cac6">ec::Loop::onBeforeStart</a></div><div class="ttdeci">virtual bool onBeforeStart()</div><div class="ttdoc">启动前触发 </div><div class="ttdef"><b>Definition:</b> loop.cpp:111</div></div>
<div class="ttc" id="classec_1_1Loop_html_ae60730f9a1e07de6039de8008293769a"><div class="ttname"><a href="classec_1_1Loop.html#ae60730f9a1e07de6039de8008293769a">ec::Loop::id</a></div><div class="ttdeci">uint32_t id() const </div><div class="ttdoc">获得自动生成的Id号 </div><div class="ttdef"><b>Definition:</b> loop.h:42</div></div>
<div class="ttc" id="classec_1_1Loop_html_a476329b5b1b32a9d91957fdf820ed35b"><div class="ttname"><a href="classec_1_1Loop.html#a476329b5b1b32a9d91957fdf820ed35b">ec::Loop::wait</a></div><div class="ttdeci">void wait()</div><div class="ttdoc">等待时间循环(线程)结束 </div><div class="ttdef"><b>Definition:</b> loop.cpp:85</div></div>
<div class="ttc" id="classec_1_1Loop_html_ab50d820e5c09ef53af2aa97a1912025a"><div class="ttname"><a href="classec_1_1Loop.html#ab50d820e5c09ef53af2aa97a1912025a">ec::Loop::onBeforeLoop</a></div><div class="ttdeci">virtual void onBeforeLoop()</div><div class="ttdoc">事件循环运行前触发 </div><div class="ttdef"><b>Definition:</b> loop.cpp:116</div></div>
<div class="ttc" id="classec_1_1Loop_html_a134b0eb8eaa83270eb375df11160bea5"><div class="ttname"><a href="classec_1_1Loop.html#a134b0eb8eaa83270eb375df11160bea5">ec::Loop::isThread</a></div><div class="ttdeci">bool isThread() const </div><div class="ttdoc">是否是以新线程在运行 </div><div class="ttdef"><b>Definition:</b> loop.h:48</div></div>
<div class="ttc" id="classec_1_1Loop_html_a9d3fec1a0d05dfdb73ceee20b6fd55b0"><div class="ttname"><a href="classec_1_1Loop.html#a9d3fec1a0d05dfdb73ceee20b6fd55b0">ec::Loop::onAfterLoop</a></div><div class="ttdeci">virtual void onAfterLoop()</div><div class="ttdoc">事件循环结束后触发 </div><div class="ttdef"><b>Definition:</b> loop.cpp:120</div></div>
<div class="ttc" id="classec_1_1Loop_html_a5e4e5650fbd4f1716429205c84ca51fd"><div class="ttname"><a href="classec_1_1Loop.html#a5e4e5650fbd4f1716429205c84ca51fd">ec::Loop::start</a></div><div class="ttdeci">bool start(bool newThread=true)</div><div class="ttdoc">启动事件循环 </div><div class="ttdef"><b>Definition:</b> loop.cpp:67</div></div>
<div class="ttc" id="classec_1_1Loop_html_af62f9eae725eda092cc52168bdcb9b53"><div class="ttname"><a href="classec_1_1Loop.html#af62f9eae725eda092cc52168bdcb9b53">ec::Loop::ev</a></div><div class="ttdeci">event_base * ev() const </div><div class="ttdoc">获取event_base *指针 </div><div class="ttdef"><b>Definition:</b> loop.h:36</div></div>
<div class="ttc" id="classec_1_1Loop_html"><div class="ttname"><a href="classec_1_1Loop.html">ec::Loop</a></div><div class="ttdoc">事件循环，对event_base的封装。 </div><div class="ttdef"><b>Definition:</b> loop.h:23</div></div>
<div class="ttc" id="classec_1_1Loop_html_aa52a4ac0232b307a08e7857748ebdd3f"><div class="ttname"><a href="classec_1_1Loop.html#aa52a4ac0232b307a08e7857748ebdd3f">ec::Loop::onAfterStop</a></div><div class="ttdeci">virtual void onAfterStop()</div><div class="ttdoc">停止后触发 </div><div class="ttdef"><b>Definition:</b> loop.cpp:124</div></div>
<div class="ttc" id="classec_1_1Loop_html_abb797bdabdefdff5b6cc3f2b1b249747"><div class="ttname"><a href="classec_1_1Loop.html#abb797bdabdefdff5b6cc3f2b1b249747">ec::Loop::curLoop</a></div><div class="ttdeci">static Loop * curLoop()</div><div class="ttdoc">返回当前线程的Loop </div><div class="ttdef"><b>Definition:</b> loop.cpp:128</div></div>
<div class="ttc" id="classec_1_1Loop_html_aa96af9467ac7882e3cea7a4dcb51005a"><div class="ttname"><a href="classec_1_1Loop.html#aa96af9467ac7882e3cea7a4dcb51005a">ec::Loop::stop</a></div><div class="ttdeci">void stop(bool waiting=true)</div><div class="ttdoc">停止事件循环 </div><div class="ttdef"><b>Definition:</b> loop.cpp:93</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Tue Jul 12 2016 14:15:08 for ec by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.6
</small></address>
</body>
</html>
